/**
 * @param {string} key  cookie的键
 * @param {string} value  cookie的值
 * @param {string} seconds  设置cookie的存活秒数
 * @param {string} path  设置cookie的路径
 */

function setCookie(key, value, seconds, path = '/') {
    let date = new Date()
    date.setTime(date.getTime() - 8 * 3600 * 1000 + seconds * 1000)
    document.cookie = `${key}=${value};expires=${date};path=${path}`
}

/**
 * @param {string} key cookie的键
 * @returns {string} cookie键对应的值
 */

function getCookie(key) {
    let cookie = document.cookie
    if (!cookie) {
        return
    }
    let cookieArr = cookie.split('; ')
    for (let i = 0; i < cookieArr.length; i++) {
        let brr = cookieArr[i].split('=')
        if (brr[0] === key) {
            return brr[1]
        }
    }
}

/**
 * 删除cookie的函数
 * @param {string} key cookie的键
 * @param {string} path cookie的路径
 */

function removeCookie(key, path = '/') {
    setCookie(key, null, -1, path)
}
